package org.apache.cordova.wu;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;

/**
 * 
 * @author 吴Sir 
 * @date 16-05-20.
 *
 */
public class NaviManager {

	public static final int CONTENTER_ID = 0x455;

	public static final String ANDROID_ASSET = "file:///android_asset/www/";

	private FragmentActivity cordovaActivity;

	public NaviManager(Activity activity) {
		this.cordovaActivity = (FragmentActivity) activity;
	}

	public void action(String url) {
		if (url.startsWith("home")) {
			home(url);
		} else if (url.startsWith("back?")) {
			int num = 0;
			String numStr = url.substring(5);
			try {
				num = Integer.valueOf(numStr);
			} catch (NumberFormatException e) {
				// TODO: handle exception
			}
			if (num > 0) {
				back(url, num);
			}
		} else if (url.startsWith("back")) {
			back(url, 1);
		} else {
			go(url);
		}
	}

	/**
	 * 前进打开新页面
	 * 
	 * @param url 页面地址
	 */
	public void go(String url) {
		if (cordovaActivity == null) {
			return ;
		}
		cordovaActivity.findViewById(NaviManager.CONTENTER_ID).setVisibility(View.VISIBLE);
		
		FragmentManager fm = cordovaActivity.getSupportFragmentManager();
		List<Fragment> fragments = fm.getFragments();
		int count = fm.getBackStackEntryCount();
		if (fragments != null && count >0) {
				// 当前页面退出
				CordovaFragment fragment = (CordovaFragment) fragments.get(count-1);
				if (fragment != null) {
					fragment.saveViewAnim();
				}
		} else {
			View view = cordovaActivity.findViewById(100);//100 是主界面的View ID
			saveViewAnim(view);
		}
		
		CordovaFragment fragment = new CordovaFragment();
		Bundle args = new Bundle();
		args.putString("url", ANDROID_ASSET+url);
		fragment.setArguments(args);
		FragmentTransaction ft = fm.beginTransaction();
		ft.add(CONTENTER_ID, fragment);
		ft.addToBackStack(url);
		ft.commit();
	}

	/**
	 * 返回前N页
	 * 
	 * @param url 页面地址
	 * @param num 返回页面数
	 */
	public void back(String url, int num) {
		FragmentManager fm = cordovaActivity.getSupportFragmentManager();
		List<Fragment> fragments = fm.getFragments();
		if (fragments != null) {
			int count = fm.getBackStackEntryCount();
			if (num > count) {
				num = count;
			}
			
			if (num == 1) {			// 返回一页时
				// 当前页面退出
				CordovaFragment fragment = (CordovaFragment) fragments.get(count-1);
				if (fragment != null) {
					fragment.exitViewAnim();
				}
				
				// 上一页面还原动画
				if ((count - 2) >= 0) {
					CordovaFragment historyFragment = (CordovaFragment) fragments.get(count-2);
					if (historyFragment != null) {
						historyFragment.restoreViewAnim();
					}
				} else {
					View view = cordovaActivity.findViewById(100);//100 是主界面的View ID
					restoreViewAnim(view);
				}
			} else  if (num > 1) {	// 返回大于一页时
				for (int i = (count-1), j=1; j<=num; i--, j++) {
					CordovaFragment fragment = (CordovaFragment) fragments.get(i);
					FragmentTransaction ft = fm.beginTransaction();
					ft.remove(fragment);
					ft.commitAllowingStateLoss();
					fm.popBackStack();
				}
			}
			
		}
	}

	/**
	 * 返回到主界面
	 * 
	 * @param url 页面地址
	 */
	public void home(String url) {
		FragmentManager fm = cordovaActivity.getSupportFragmentManager();
		List<Fragment> fragments = fm.getFragments();
		if (fragments != null) {
			int count = fm.getBackStackEntryCount();
			
			for (int i = (count-1); i >=0; i--) {
				CordovaFragment fragment = (CordovaFragment) fragments.get(i);
				FragmentTransaction ft = fm.beginTransaction();
				ft.remove(fragment);
				ft.commitAllowingStateLoss();
				fm.popBackStack();
			}
//			// 主界面还原动画
//			View view = cordovaActivity.findViewById(100);// 100 是主界面的View ID
//			restoreViewAnim(view);
		}
	}

	/**
	 * View进入后台动画
	 */
	private void saveViewAnim(View view) {
		if (view != null) {
			WindowManager windowManager = (WindowManager)  cordovaActivity.getSystemService(Context.WINDOW_SERVICE);
			DisplayMetrics outMetrics = new DisplayMetrics();
			windowManager.getDefaultDisplay().getMetrics(outMetrics);
			float offset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, outMetrics);
			TranslateAnimation animation = new TranslateAnimation(0, -offset, 0, 0);
			animation.setDuration(500);
			view.startAnimation(animation);
		}
	}

	/**
	 * View还原动画
	 */
	private void restoreViewAnim(View view) {
		if (view != null) {
			WindowManager windowManager = (WindowManager)  cordovaActivity.getSystemService(Context.WINDOW_SERVICE);
			DisplayMetrics outMetrics = new DisplayMetrics();
			windowManager.getDefaultDisplay().getMetrics(outMetrics);
			float offset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, outMetrics);
			TranslateAnimation animation = new TranslateAnimation(-offset, 0, 0, 0);
			animation.setDuration(500);
			view.startAnimation(animation);
		}
	}

}
